//index.js
//获取应用实例
var app = getApp();
var config = require('../../utils/config');
var url = config.url;
var util = require('../../utils/util');
var db = require('../../utils/db');

Page({
  data: {
    bookList: [],
    inputValue: '',
    isSearch:false
  },
  inputChange: function (e) {
    this.data.inputValue = e.detail.value;
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    //console.log('onload index')
    var that = this;
  },

 
  queryBooks: function (e) {
    var that = this;

    var inputMsg = that.data.inputValue;
    var options = {
      url: config.clubApi.list,
      data: {
        appkey: config.appKey,
        type: 'bookLibrary',
        // columns:'title',
        keywords: inputMsg
        //columns: ['id', 'isbn', 'title']
      }
    };

    util.request(options, (res, err) => {
      if (res.data.result.length == 0) {
        console.log('没有找到')
        that.setData({
          bookList: [],
          isSearch:true
        });
      } else {
        var books = [];
        for (var i = 0; i < res.data.result.length; i++) {
          books.push(res.data.result[i]);
        }
        that.setData({
          bookList: books,
          isSearch:false
        });
      }
    });
  },
  goToDetailPage: function (e) {
    var isbn = e.currentTarget.id;
    var num = e.currentTarget.dataset.num;
    wx.navigateTo({
      url: '../detail/detail?id=' + isbn + '&num=' + num
    });
  },
  onShow: function () {
    // 页面显示
    //console.log('onshow');
    this.queryAllBooks();
    // this.queryBooks();
  },
  queryAllBooks: function () {

    var that = this;
    var options = {
      url: config.clubApi.list,
      data: {
        appkey: config.appKey,
        type: 'bookLibrary'
      }
    };

    util.request(options, function (res) {
      var books = [];
      for (var i = 0; i < res.data.result.length; i++) {
        books.push(res.data.result[i]);
      }
      that.setData({
        bookList: books
      })
    });
  }
})
